#include "server_thread.h"
#if 11200 <= ERPC_VERSION_NUMBER
#include "c_lvgl_is_designer_server.h"
#else
#include "lvgl_is_designer_server.h"
#endif
#include "lvgl_isd_resource.h"

#include <QTimer>

ServerThread::ServerThread(QObject *parent) : QObject(parent)
{

}

void ServerThread::startSVC()
{
    lvgl_isd_resource_init();

    transport = erpc_transport_tcp_init("127.0.0.1", 5555, true);
    erpc_mbf_t message_buffer_factory = erpc_mbf_dynamic_init();
    server = erpc_server_init(transport, message_buffer_factory);

#if 11200 <= ERPC_VERSION_NUMBER
    erpc_add_service_to_server(server, create_ISDesigner_service());
#else
    erpc_add_service_to_server(create_ISDesigner_service());
#endif

    m_timer = new QTimer(this);
    m_timer->setInterval(1);
    connect(m_timer, &QTimer::timeout, this, &ServerThread::runServer);
    m_timer->start();
}

void ServerThread::stopSVC()
{
#if 11200 <= ERPC_VERSION_NUMBER
    erpc_transport_tcp_close(transport);
#else
    erpc_transport_tcp_close();
#endif
}

void ServerThread::runServer()
{
#if 11200 <= ERPC_VERSION_NUMBER
    erpc_server_run(server);
#else
    erpc_server_run();
#endif
}
